﻿' Licensed under GNU General Public License version 2 (GPLv2) 
' Copyright (C) 1989, 1991 Free Software Foundation, Inc.
'
' Everyone is permitted to copy and distribute verbatim copies
' of this license document, but changing it is not allowed.

Imports System
Imports System.Collections.Specialized
Imports System.Text.RegularExpressions

''' <summary>
''' Arguments class.
''' </summary>
''' <remarks>Helper class for parsing arguments.</remarks>
Public Class Arguments

    Private _parameters As StringDictionary

#Region "Constructor"
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="args"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal args As String())
        _parameters = New StringDictionary()
        Dim Spliter As Regex = New Regex("^-{1,2}|=", RegexOptions.IgnoreCase Or RegexOptions.Compiled)
        'Dim Spliter As Regex = New Regex("^-{1,2}|^/|=|:", RegexOptions.IgnoreCase Or RegexOptions.Compiled)

        Dim Remover As Regex = New Regex("^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase Or RegexOptions.Compiled)

        Dim Parameter As String = Nothing
        Dim Parts() As String

        ' Valid parameters forms:
        ' {-,/,--}param{ ,=,:}((",')value(",'))
        ' Examples: 
        ' -param1 value1 --param2 /param3:"Test-:-work" 
        '   /param4=happy -param5 '--=nice=--'

        Dim Txt As String
        For Each Txt In args
            ' Look for new parameters (-,/ or --) and a
            ' possible enclosed value (=,:)            
            ' Look for new parameters (-,/ or --) and a 
            ' possible enclosed value (=,:) 
            Parts = Spliter.Split(Txt, 3)

            Select Case Parts.Length
                ' Found a value (for the last parameter 
                ' found (space separator)) 
                Case 1
                    If Parameter IsNot Nothing Then
                        If Not _parameters.ContainsKey(Parameter) Then
                            Parts(0) = Remover.Replace(Parts(0), "$1")

                            _parameters.Add(Parameter, Parts(0))
                        End If
                        Parameter = Nothing
                    End If
                    ' else Error: no parameter waiting for a value (skipped) 
                    Exit Select

                    ' Found just a parameter 
                Case 2
                    ' The last parameter is still waiting. 
                    ' With no value, set it to true. 
                    If Parameter IsNot Nothing Then
                        If Not _parameters.ContainsKey(Parameter) Then
                            _parameters.Add(Parameter, "true")
                        End If
                    End If
                    Parameter = Parts(1)
                    Exit Select

                    ' Parameter with enclosed value 
                Case 3
                    ' The last parameter is still waiting. 
                    ' With no value, set it to true. 
                    If Parameter IsNot Nothing Then
                        If Not _parameters.ContainsKey(Parameter) Then
                            _parameters.Add(Parameter, "true")
                        End If
                    End If

                    Parameter = Parts(1)

                    ' Remove possible enclosing characters (",') 
                    If Not _parameters.ContainsKey(Parameter) Then
                        Parts(2) = Remover.Replace(Parts(2), "$1")
                        _parameters.Add(Parameter, Parts(2))
                    End If

                    Parameter = Nothing
                    Exit Select
            End Select
        Next
        ' In case a parameter is still waiting
        If Parameter IsNot Nothing Then
            If Not _parameters.ContainsKey(Parameter) Then
                _parameters.Add(Parameter, "true")
            End If
        End If
    End Sub
#End Region

#Region "Public properties"
    ''' <summary>
    ''' Retrieve a parameter value if it exists.
    ''' </summary>
    ''' <param name="Param"></param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Default Public ReadOnly Property Item(ByVal Param As String) As String
        Get
            Return (_parameters(Param))
        End Get
    End Property
#End Region
End Class
